﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Management;
using System.Text.RegularExpressions;

namespace GDIT.Base.PageBaseHelper
{
    /// <summary>
    /// PageBase页面基类
    /// </summary>
    public class PageBase:Page
    {
        #region [获取客户端IP地址]
        /// <summary>
        /// 获取客户端IP地址
        /// </summary>
        /// <returns></returns>
        public static string GetClientIP()
        {
            String result = String.Empty;
            result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (result == null || result == string.Empty)
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            if (result == null || result == string.Empty)
            {
                result = HttpContext.Current.Request.UserHostAddress;
            }
            if (result == null || result == string.Empty)
            {
                return "0.0.0.0";
            }
            return result;
        }
#endregion

        #region [获取客户端MAC]
        /// <summary>
        /// 获取客户端MAC
        /// </summary>
        /// <returns></returns>
        public static string GetClientMAC()
        {
            using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
            {
                try {
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        if ((bool)mo["IPEnabled"])
                        {
                            return mo["MacAddress"].ToString();
                        }
                    }
                }catch(Exception e){
                    
                }
                return null;
            }
        }
        #endregion
        #region [错误显示]
        /// <summary>
        /// 错误显示
        /// </summary>
        /// <param name="messages"></param>
        public static void ShowErrorPage(string messages)
        {
            string[] args = { "TITLE", "URL", "MESSAGES", "SITENAME" };
            //错误页面
            string filePath = "Error.html";
            if (System.IO.File.Exists(HttpContext.Current.Server.MapPath("根目录路径")))
            {
                //尚未完成
            }
           
        }

        #region 获取web项目的各种路径
        /// <summary>
        /// 取得网站的根目录的URL
        /// </summary>
        /// <returns></returns>
        public static string GetRootURI()
        {
            string AppPath = string.Empty;
            HttpContext HttpCurrent = HttpContext.Current;
            HttpRequest Req;
            if (HttpCurrent != null)
            {
                Req = HttpCurrent.Request;

                string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority);
                if (Req.ApplicationPath == null || Req.ApplicationPath == "/")
                    //直接安装在 Web 站点 
                    AppPath = UrlAuthority;
                else
                    //安装在虚拟子目录下 
                    AppPath = UrlAuthority + Req.ApplicationPath;
            }
            return AppPath;
        }
        /// <summary>
        /// 取得网站的根目录的URL
        /// </summary>
        /// <param name="Req"></param>
        /// <returns></returns>
        public static string GetRootURI(HttpRequest Req)
        {
            string AppPath = string.Empty;
            if (Req != null)
            {
                string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority);
                if (Req.ApplicationPath == null || Req.ApplicationPath == "/")
                    //直接安装在 Web 站点 
                    AppPath = UrlAuthority;
                else
                    //安装在虚拟子目录下 
                    AppPath = UrlAuthority + Req.ApplicationPath;
            }
            return AppPath;
        }
        /// <summary>
        /// 取得网站根目录的物理路径
        /// </summary>
        /// <returns></returns>
        public static string GetRootPath()
        {
            string AppPath = string.Empty;
            HttpContext HttpCurrent = HttpContext.Current;
            if (HttpCurrent != null)
            {
                AppPath = HttpCurrent.Server.MapPath("~");
            }
            else
            {
                AppPath = AppDomain.CurrentDomain.BaseDirectory;
                if (Regex.Match(AppPath, @"\\$", RegexOptions.Compiled).Success)
                    AppPath = AppPath.Substring(0, AppPath.Length - 1);
            }
            return AppPath;
        }


        #endregion
       
        #endregion

        #region 客户端输出流
        /// <summary>
        /// 对ClientScript进行封装的方法，用于HTML标签的输出
        /// </summary>
        /// <param name="html"></param>
        /// <param name="isShowScriptTag"></param>
        public static void HtmlResponseWrite(string html,bool isShowScriptTag,Page page)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "", html, true);
        }
        #endregion

        #region 客户端弹出框
        /// <summary>
        /// 客户端弹出提示信息
        /// </summary>
        /// <param name="str"></param>
        public static void MessageBoxShow(string str,Page page)
        {
              HtmlResponseWrite(String.Format("alert('{0}')", str), true,page);
                //page.ClientScript.RegisterStartupScript(page.GetType(), "", String.Format("alert('{0}')", str), true);     
        }

        /// <summary>
        /// 客户端弹出提示信息并且跳转
        /// </summary>
        /// <param name="str"></param>
        /// <param name="url"></param>
        public static void MessageBoxShowAndGo(string str, string url,Page page)
        {
            string scripts = String.Format("alert('{0}');window.location.href='{1}'", str, url);
            //page.ClientScript.RegisterStartupScript(page.GetType(), "", scripts, true);
            HtmlResponseWrite(scripts,true,page);
        }

        /// <summary>
        /// 客户端用Confirm进行页面跳转的判断
        /// </summary>
        /// <param name="str"></param>
        /// <param name="okUrl"></param>
        /// <param name="cancelUrl"></param>
        public static void MessageBoxShowAndConfirmUrl(string str, string okUrl, string cancelUrl)
        {
            using (Page page=new Page())
            {
                string scripts = String.Empty;
                if (string.IsNullOrEmpty(cancelUrl))
                {
                    scripts = String.Format("if(confirm('{0}')==true){{window.location.href='{1}'}}else{{return false}}", str, okUrl);
                }
                else
                {
                    scripts = String.Format("if(confirm('{0}')==true){{window.location.href='{1}'}}else{{window.location.href='{2}'}}", str, okUrl, cancelUrl);
                }
                HtmlResponseWrite(okUrl, true,page);
                //page.ClientScript.RegisterStartupScript(page.GetType(), "", okUrl, true);

            }
            
        }
        #endregion
    }
}
